import lib.problem as problem
import unittest

class Problem022(problem.Problem):
	def __init__(self):
		problem.Problem.__init__(self, 22)
		
	def listNames(self, filePath):
		filePath = self.getEtcDir() + "/" + filePath
		file = open(filePath)
		namesStr = file.read()
		names = namesStr.split('","')
		names[0] = names[0][1:] 
		names[-1] = names[-1][:-1]
		return names
	
	def getSortesNames(self,filePath):
		names = self.listNames(filePath)
		return sorted(names)
	
	def getNameScore(self, name, pos):
		sum = 0
		for c in name.upper():
			sum += ord(c) - 64
		
		return sum * pos
	
	def getSumOfScore(self, filePath):
		names = self.getSortesNames(filePath)
		sum = 0
		pos = 1
		
		for name in names:			
			sum += self.getNameScore(name, pos)
			pos += 1
		
		return sum
		
	def solve(self):
		return self.getSumOfScore("names.txt")
	
	
	
class Problem022Tester(unittest.TestCase):
	
	def setUp(self):
		self.p022 = Problem022()
		
	def testListNames(self):
		names = self.p022.listNames("names.txt")
		self.assertAlmostEquals(5163, len(names))
		
	def testSortNames(self):
		sortedNames = self.p022.getSortesNames("names.txt")
		self.assertEquals(5163, len(sortedNames))
		self.assertEquals('AARON', sortedNames[0])
		self.assertEquals('COLIN', sortedNames[937])
		self.assertEquals('ZULMA', sortedNames[-1])
		
		
	def testGetNameScore(self):
		score = self.p022.getNameScore("COLIN", 938)
		self.assertEquals(49714, score)
		
	def testGetSumOfScore(self):
		score = self.p022.getSumOfScore("names.txt")
		self.assertEquals(871198282, score)
	
#	def testSolve(self):
#		self.assertEquals(-1, self.p022.solve())
